<?php  // $Id: psychometricgraph.php,v 1 2010/12/02 13:12:00 tjhunt Exp $
include '../../../../config.php';
include $CFG->dirroot."/lib/graphlib.php";
include $CFG->dirroot."/mod/quiz/report/reportlib.php";
function graph_get_new_colour(){
    static $colourindex = 14;
    $colours = array('red', 'green', 'yellow', 'orange', 'purple', 'black', 'maroon', 'blue', 'ltgreen', 'navy', 'ltred', 'ltltgreen', 'ltltorange', 'olive', 'gray', 'ltltred', 'ltorange', 'lime', 'ltblue', 'ltltblue');
    $colour = $colours[$colourindex];
    $colourindex++;
    if ($colourindex > (count($colours)-1)){
        $colourindex =0;
    }
    return $colour;
}

$question = $_SESSION['question'];

define('QUIZ_REPORT_MAX_PARTICIPANTS_TO_SHOW_ALL_GROUPS', 500);

$line = new graph(360,270);
$line->parameter['title']   = '';
$line->parameter['y_label_left'] = get_string('xlabelquizchart', 'quiz_psychometric');
$line->parameter['y_label_angle'] = 90;
$line->parameter['x_label_angle'] = 0;
$line->parameter['x_axis_angle'] = 0;

//following two lines seem to silence notice warnings from graphlib.php
$line->y_tick_labels = null;
$line->offset_relation = null;

$line->parameter['bar_size']    = 0.5; // will make size > 1 to get overlap effect when showing groups
$line->parameter['bar_spacing'] = 10; // don't forget to increase spacing so that graph doesn't become one big block of colour

//pick a sensible number of bands depending on quiz maximum grade.
$bands = count($question);
while ($bands >= 20 || $bands < 10){
    if ($bands >= 50){
        $bands = $bands /5;
    } else if ($bands >= 20) {
        $bands = $bands /2;
    }
    if ($bands < 4){
        $bands = $bands * 5;
    } else if ($bands < 10){
        $bands = $bands * 2;
    }
}

$bandwidth = count($question) / $bands;
$bands = ceil($bands);
$bandlabels = array();

$count = 1;
foreach($question as $q)
{
	$bandlabels[] =$count;
	$count++;
}

$line->x_data          = $bandlabels;

$ylabels = array();
foreach($question as $q)
	$ylabels[] =$q->easilyindex;

$line->y_format['allusers'] =
  array('colour' => graph_get_new_colour(), 'bar' => 'fill', 'shadow_offset' => 1, 'legend' => get_string('allparticipants'));
$line->y_data['allusers'] = $ylabels;
$line->y_order = array('allusers');


$line->parameter['y_min_left'] = 0;  // start at 0
$line->parameter['y_max_left'] = max($ylabels); 
$line->parameter['y_decimal_left'] = 0; // 2 decimal places for y axis.


//pick a sensible number of gridlines depending on max value on graph.
$gridlines = max($line->y_data['allusers']);
while ($gridlines >= 10){
    if ($gridlines >= 50){
        $gridlines = $gridlines /5;
    } else {
        $gridlines = $gridlines /2;
    }
}

$line->parameter['y_axis_gridlines'] = $gridlines+1; 
$line->draw();

?>
